home *** CD-ROM | disk | FTP | other *** search
- /*
- * Stopwatch/time support for Stopwatch app.
- * Author: Rich Plevin
- *
- * For legal stuff see the file COPYRIGHT
- */
- #import "StopWatch.h"
-
- @implementation StopWatch
-
- - startWatch
- {
- if ( running == YES )
- return nil;
-
- gettimeofday( &start, NULL );
- running = YES;
- return self;
- }
-
- - stopWatch
- {
- if ( running == NO )
- return nil;
-
- /* Note: Second arg (timezone) is ignored on NeXT */
- gettimeofday( &finish, NULL );
- running = NO;
- return self;
- }
-
- - (BOOL) running
- {
- return running;
- }
-
- /*
- * If its running, show time from start until now, otherwise show
- * the time that passed between the saved start and finish.
- */
- - (int) elapsedMinutes
- {
- int mins, secs;
- struct timeval end;
-
- if ( running == YES )
- gettimeofday( &end, NULL );
- else
- end = finish;
-
- secs = end.tv_sec - start.tv_sec;
- mins = secs / 60;
- if ( secs % 60 >= 30 )
- ++mins; /* round up if necessary */
-
- return mins;
- }
-
- - (const char *) elapsedTime
- {
- int hrs, mins;
- static char buf[6]; /* enough for a "hh:mm" string - a 99 hour binge... */
-
- mins = [self elapsedMinutes];
-
- hrs = mins / 60; /* total number of hours */
- mins %= 60; /* remainder number of minutes */
-
- /*
- * NB: Make the buffer larger if the format string requires it!!
- */
- sprintf( buf, "%02d:%02d", hrs, mins );
- return (const char *)buf;
- }
-
- - (const char *)startTimeString
- {
- static char buf[6]; /* enough for a "hh:mm" string */
- struct tm *tm;
-
- tm = localtime(&start.tv_sec);
- sprintf( buf, "%02d:%02d", tm->tm_hour, tm->tm_min );
- return (const char *)buf;
- }
-
- - (const char *)startDateString;
- {
- static char buf[9]; /* enough for a "mm/dd/yy" string */
- struct tm *tm;
-
- tm = localtime(&start.tv_sec);
- sprintf( buf, "%02d/%02d/%02d", tm->tm_mon + 1, tm->tm_mday, tm->tm_year );
- return (const char *)buf;
- }
-
- @end
-